# -*- coding: utf-8 -*-

"""
对网络数据进行处理，包括工参、kpi统计、告警、MRO

"""
import pandas as pd
import shapely.wkt
import autonei.geometry as geo


def oracle2df(sql=None, con=None):
    import cx_Oracle

    if sql is None:
        sql = r"select t.*,to_char(sdo_util.to_wktgeometry(t.spolygon)) from CFG_SITEINFO_TDLTE t where  t.longitude between 121.563819 and 121.641705 and t.latitude between 31.154215 and  31.205380"

    if con is None:
        con = r'fast/Fast*123@192.168.43.13/orclpdb'
    con = r'fast/Fast*123@192.168.61.161/fast'

    db = cx_Oracle.connect(con)
    # df = pd.read_sql(sql, db)
    # df.columns = [x.lower() for x in df.columns]
    df = pd.read_sql(sql, db)
    df.columns = [x.lower() for x in df.columns]
    return df

df=oracle2df()
df.to_csv('d:\cellinfo.csv',encoding='gbk')


def add_cell_xy(cellinfo, azimuth='azimuth', distance=10, sitetype='outdoor'):
    '''在cellinfo dataframe中增加两列沿方位角偏移后的cell经纬度，分别是x_cell、y_cell

    :param cellinfo: cellinfo dataframe
    :param azimuth: 偏移方位值所在的列名
    :param distance: 偏移距离
    :param sitetype: 默认只偏移宏站
    :return: cellinfo dataframe
    '''
    x = []
    y = []
    for index, row in cellinfo.iterrows():
        if row['sitetype'].lower() == sitetype:
            x1, y1 = geo.XY(row['longitude'], row['latitude']).offset(row[azimuth], distance * 1.4142 / 1000)
        else:
            x1, y1 = row['longitude'], row['latitude']
        x.append(x1)
        y.append(y1)
    cellinfo['x_cell'] = x
    cellinfo['y_cell'] = y
    return cellinfo


